package com.bcxj.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bcxj.internalcommon.constant.CommonStatusEnum;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.PriceRule;
import com.bcxj.serviceprice.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: PriceRuleService
 * Package: com.bcxj.serviceprice.service
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/9 23:40
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class PriceRuleService {

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    /**
     * 增加计价规则
     * @param priceRule
     * @return
     */
    public ResponseResult add(PriceRule priceRule) {
        // 获取fareType
        String fareType = priceRule.getCityCode() + "$" +  priceRule.getVehicleType();
        priceRule.setFareType(fareType);
        // 查询是否有旧的计价规则
        List<PriceRule> priceRules = priceRuleMapper.selectList(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getCityCode, priceRule.getCityCode())
                .eq(PriceRule::getVehicleType, priceRule.getVehicleType())
                .orderByDesc(PriceRule::getFareVersion));
        Integer fareVersion = 0;
        if(priceRules != null && priceRules.size() > 0) {
            // 计价规则已存在
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS);
        }

        priceRule.setFareVersion(++fareVersion);

        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    /**
     * 计价规则修改
     * @param priceRule
     * @return
     */
    public ResponseResult edit(PriceRule priceRule) {
        // 获取fareType
        String fareType = priceRule.getCityCode() + "$" +  priceRule.getVehicleType();
        priceRule.setFareType(fareType);
        // 查询是否有旧的计价规则
        List<PriceRule> priceRules = priceRuleMapper.selectList(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getCityCode, priceRule.getCityCode())
                .eq(PriceRule::getVehicleType, priceRule.getVehicleType())
                .orderByDesc(PriceRule::getFareVersion));
        Integer fareVersion = 0;
        if(priceRules == null || priceRules.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY);
        }
        if(priceRules != null && !priceRules.isEmpty()) {
            PriceRule priceRuleMax = priceRules.get(0);
            // 判断计价规则是否已经修改
            if(priceRuleMax.getUnitPricePerMile().equals(priceRule.getUnitPricePerMile())
            && priceRuleMax.getUnitPricePerMinute().equals(priceRule.getUnitPricePerMinute())
            && priceRuleMax.getStartFare().equals(priceRule.getStartFare())
            && priceRuleMax.getStartMile().equals(priceRule.getStartMile())) {
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT);
            }

            // 得到最高版本的版本号
            fareVersion = priceRuleMax.getFareVersion();
        }

        priceRule.setFareVersion(++fareVersion);

        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }


    /**
     * 获取最新的计价规则
     * @param fareType
     * @return
     */
    public ResponseResult<PriceRule> getNewestVersion(String fareType) {
        // 1 获取最新的计价规则
        List<PriceRule> priceRules = priceRuleMapper.selectList(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getFareType, fareType)
                .orderByDesc(PriceRule::getFareVersion));

        // 2.1. 判断数据
        if(priceRules != null && priceRules.size() > 0) {
            // 2 返回最新的计价规则
            return ResponseResult.success(priceRules.get(0));
        }else {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY);
        }
    }


    /**
     * 判断计价规则是否是最新的
     * @param fareType
     * @param fareVersion
     * @return
     */
    public ResponseResult<Boolean> isNew(String fareType, Integer fareVersion) {
        // 查询数据
        ResponseResult<PriceRule> newestVersion = getNewestVersion(fareType);
        PriceRule data = newestVersion.getData();
        if(newestVersion.getCode().equals(CommonStatusEnum.PRICE_RULE_EMPTY.getCode())) {
            return ResponseResult.success(false);
//            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY);
        }

        // 是否存在
        if(data.getFareVersion() > fareVersion) {
            return ResponseResult.success(false);
        }

        return ResponseResult.success(true);
    }


    /**
     * 计价规则是否存在
     * @param address
     * @param vehicleType
     * @return
     */
    public ResponseResult<Boolean> ifExists(String address, String vehicleType) {

        List<PriceRule> priceRules = priceRuleMapper.selectList(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getCityCode, address)
                .eq(PriceRule::getVehicleType, vehicleType));

        if(priceRules.isEmpty()) {
            return ResponseResult.success(false);
        }
        return ResponseResult.success(true);
    }
}
